from string import Template

with open('exploit/target_values.txt', 'r') as f:
    targets = f.read().split('|')

rand_context = {
    'A': targets[2].split(':')[1],
    'B': targets[3].split(':')[1],
    'C': targets[4].split(':')[1],
    'D': targets[5].split(':')[1],
    'E': targets[6].split(':')[1],
    'F': targets[7].split(':')[1],
    'G': targets[8].split(':')[1],
    'H': targets[9].split(':')[1],
    'I': targets[10].split(':')[1],
    'J': targets[11].split(':')[1],
}

exploit_context = {
    'K': targets[1].split(':')[1],
    'L': targets[0].split(':')[1].split('.')[0],
}



with open('base-files/test_exploit_TEMPLATE.php', 'r') as f:
    src = Template(f.read())
    result = src.safe_substitute(rand_context)
with open('exploit/test_exploit.php', 'w') as f:
    f.write(result)

with open('base-files/exploit_TEMPLATE.c', 'r') as f:
    src = Template(f.read())
    result = src.safe_substitute(exploit_context)
with open('exploit/exploit.c', 'w') as f:
    f.write(result)


print()
print('RANDOM NUMBERS:')
for i in range(0,10):
    print('\t%s' % (targets[2+i].split(':')[1]))
print()

print("PID & TIME:")
print('\tint pid = %s;' % (targets[1].split(':')[1]))
print('\tlong epoch = %s;' % (targets[0].split(':')[1].split('.')[0]))
print()
